package action;

import config.ApplicationConfig;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import server.LoginServer;
import server.SettingServer;
import util.Decode;
import util.Encode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-04-15 Monday 08:15:27
 */
public class FootServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        HttpSession session = request.getSession();
        String username = session.getAttribute("user") == null ? "" : session.getAttribute("user").toString();
        String cookie = session.getAttribute("cookie") == null ? "" : session.getAttribute("cookie").toString();
        String page = "information.jsp";
        if (username == null || username.equals("") || cookie == null || cookie.equals("")) {
            session.setAttribute("infoTitle", "未登录");
            session.setAttribute("infoContent", "您还未登录，请先登录");
            session.setAttribute("return", "index.jsp");
            session.setAttribute("panelColor", "red");
            session.removeAttribute("user");
            session.removeAttribute("cookie");
        } else {
            LoginServer loginServer = new LoginServer();
            loginServer.open();
            if (!loginServer.checkLogin(username, cookie)) {
                String foot0Title = request.getParameter("foot0Title");
                String foot0Line0 = request.getParameter("foot0Line0");
                String foot0Line1 = request.getParameter("foot0Line1");
                String foot0Line2 = request.getParameter("foot0Line2");

                String foot1Title = request.getParameter("foot1Title");
                String foot1Line0 = request.getParameter("foot1Line0");
                String foot1Line1 = request.getParameter("foot1Line1");
                String foot1Line2 = request.getParameter("foot1Line2");

                String foot2Title = request.getParameter("foot2Title");
                String foot2Line0 = request.getParameter("foot2Line0");
                String foot2Line1 = request.getParameter("foot2Line1");
                String foot2Line2 = request.getParameter("foot2Line2");
                if (foot0Title != null && foot0Line0 != null && foot0Line1 != null && foot0Line2 != null &&
                        foot1Title != null && foot1Line0 != null && foot1Line1 != null && foot1Line2 != null &&
                        foot2Title != null && foot2Line0 != null && foot2Line1 != null && foot2Line2 != null) {

                    Decode decode = new Decode();

                    foot0Title = decode.urlUtf8Decode(foot0Title);
                    foot0Line0 = decode.urlUtf8Decode(foot0Line0);
                    foot0Line1 = decode.urlUtf8Decode(foot0Line1);
                    foot0Line2 = decode.urlUtf8Decode(foot0Line2);

                    foot1Title = decode.urlUtf8Decode(foot1Title);
                    foot1Line0 = decode.urlUtf8Decode(foot1Line0);
                    foot1Line1 = decode.urlUtf8Decode(foot1Line1);
                    foot1Line2 = decode.urlUtf8Decode(foot1Line2);

                    foot2Title = decode.urlUtf8Decode(foot2Title);
                    foot2Line0 = decode.urlUtf8Decode(foot2Line0);
                    foot2Line1 = decode.urlUtf8Decode(foot2Line1);
                    foot2Line2 = decode.urlUtf8Decode(foot2Line2);

                    Encode encode = new Encode();

                    foot0Title = encode.utf8Encode(foot0Title);
                    foot0Line0 = encode.utf8Encode(foot0Line0);
                    foot0Line1 = encode.utf8Encode(foot0Line1);
                    foot0Line2 = encode.utf8Encode(foot0Line2);

                    foot1Title = encode.utf8Encode(foot1Title);
                    foot1Line0 = encode.utf8Encode(foot1Line0);
                    foot1Line1 = encode.utf8Encode(foot1Line1);
                    foot1Line2 = encode.utf8Encode(foot1Line2);

                    foot2Title = encode.utf8Encode(foot2Title);
                    foot2Line0 = encode.utf8Encode(foot2Line0);
                    foot2Line1 = encode.utf8Encode(foot2Line1);
                    foot2Line2 = encode.utf8Encode(foot2Line2);

                    JSONArray foot = new JSONArray();

                    JSONObject temp = new JSONObject();
                    temp.put("name", "foot0");
                    temp.put("title", foot0Title);
                    temp.put("line0", foot0Line0);
                    temp.put("line1", foot0Line1);
                    temp.put("line2", foot0Line2);
                    foot.add(temp);

                    temp = new JSONObject();
                    temp.put("name", "foot1");
                    temp.put("title", foot1Title);
                    temp.put("line0", foot1Line0);
                    temp.put("line1", foot1Line1);
                    temp.put("line2", foot1Line2);
                    foot.add(temp);

                    temp = new JSONObject();
                    temp.put("name", "foot2");
                    temp.put("title", foot2Title);
                    temp.put("line0", foot2Line0);
                    temp.put("line1", foot2Line1);
                    temp.put("line2", foot2Line2);
                    foot.add(temp);

                    SettingServer settingServer = new SettingServer();
                    settingServer.open();
                    settingServer.recordSettingObject("foot", foot);
                    settingServer.close();
                    session.setAttribute("infoTitle", "设置页脚");
                    session.setAttribute("infoContent", "设置成功");
                    session.setAttribute("return", "mine");
                    session.setAttribute("panelColor", "blue");
                }
            } else {
                session.setAttribute("infoTitle", "未登录");
                session.setAttribute("infoContent", "您还未登录，请先登录");
                session.setAttribute("return", "index.jsp");
                session.setAttribute("panelColor", "red");
                session.removeAttribute("user");
                session.removeAttribute("cookie");
            }
            loginServer.close();
        }
        response.sendRedirect(page);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
